Public Partial Class FrmRemitoServicio
    Inherits System.Web.UI.Page
    Private tblRemitoServicio As CapaDeNegocios.nRemitoServicios = New CapaDeNegocios.nRemitoServicios
    Private tblRemitoServicioDetalle As CapaDeNegocios.nDetalleRemitoServicios = New CapaDeNegocios.nDetalleRemitoServicios
    Private tblListaPrecio As CapaDeNegocios.nListaDePrecio = New CapaDeNegocios.nListaDePrecio
    Private TblSumaSubTotales As CapaDeNegocios.nRemitoServicios = New CapaDeNegocios.nRemitoServicios
    Private tblValorizar As CapaDeNegocios.nRemitoServicios = New CapaDeNegocios.nRemitoServicios
    Private TblOrdenServicio As CapaDeNegocios.nOrdenServicio = New CapaDeNegocios.nOrdenServicio
    Private TblTipoUnidad As CapaDeNegocios.nTipoUnidad = New CapaDeNegocios.nTipoUnidad

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("Login") = "Anonimo" Then
            Response.Redirect("/")
        End If

        If Not Page.IsPostBack Then
            limpiarCampos()
            LnkAgregaOrdenes.OnClientClick = "javascript:var popup = window.open('FrmListaOrdenAgregar.aspx?IdOrdenServicio=" + HFOrdenServicio.ClientID + "&IDOrdenservicioDesc=" + txtOrdenServicio.ClientID + "&IdClienteDesc=" + txtCliente.ClientID + "&IdCliente=" + HFCliente.ClientID + "&IdTUnidad=" + HFTUnidad.ClientID + "&IDTipoUnidadDesc1=" + HFTipoUnidad.ClientID + "&IDTipoUnidadDesc2=" + TxtTipoUnidad.ClientID + "','popup','Width=800,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
        Else
            txtCliente.Text = HFCliente.Value
            txtOrdenServicio.Text = HFOrdenServicio.Value
            TxtTipoUnidad.Text = HFTipoUnidad.Value
        End If
    End Sub
    Sub limpiarCampos()
        Me.txtOrdenServicio.Text = ""
        Me.TxtNumero.Text = ""
        Me.DPFechaRemito.SelectedValue = DBNull.Value
        Me.txtCliente.Text = ""
        Me.txtDetalle.Text = ""
        Me.TxtHoras.Text = ""
        Me.TxtKilometros.Text = ""
        Me.TxtSubHS.Text = ""
        Me.TxtSubKM.Text = ""
        Me.TxtTotal.Text = ""
        Me.TxtValorizar.Text = ""
        Me.TxtTipoUnidad.Text = ""
        HFCodigoRemito.Value = ""
        HFTUnidad.Value = ""
        HFOrdenServicio.Value = ""
        HFListaPrecio.Value = ""
        HFCodigoRemito.Value = ""
        HFCliente.Value = ""
    End Sub
    Sub limpiarHF()
        'HFCodigoRemito.Value = ""
        'HFTUnidad.Value = ""
        'HFOrdenServicio.Value = ""
        'HFListaPrecio.Value = ""
        'HFCodigoRemito.Value = ""
        'HFCliente.Value = ""
    End Sub


    Protected Sub LinkGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkGuardar.Click
        Dim Fecha As Date
        Dim SubHS As Double
        Dim SubKM As Double
        Fecha = DPFechaRemito.SelectedValue
        Dim total As Double = 0
        Dim listaPrecio As Double = 0

        Try
            If ChkOpcionValorizacion.Checked Then
                total = TxtValorizar.Text
                SubHS = 0
                SubKM = 0
                TxtSubHS.Text = 0
                TxtSubKM.Text = 0
            Else
                SubHS = CDbl(TxtSubHS.Text)
                SubKM = CDbl(TxtSubKM.Text)
                total = TblSumaSubTotales.SumaSubtotales(SubHS, SubKM, 0)
                listaPrecio = Convert.ToInt32(HFListaPrecio.Value)
            End If
        Catch ex As Exception
        End Try
        Dim existe As Boolean = tblRemitoServicio.existe(Convert.ToInt32(TxtNumero.Text))
        If Not existe Then
            If Me.HFCodigoRemito.Value = "" And TxtTotal.Text <> "-1" Then

                Dim ok As Boolean
                ok = tblRemitoServicio.Insertar(0, Convert.ToInt32(TxtNumero.Text), Fecha, Convert.ToInt32(Me.HFOrdenServicio.Value), total, txtDetalle.Text)

                If ok Then
                    '    TblOrdenServicio.Eliminar(HFOrdenServicio.Value)
                    Dim IdRemito As Long = tblRemitoServicio.BuscarUltimoRemito()

                    tblRemitoServicioDetalle.Insertar(TxtKilometros.Text, TxtSubKM.Text, IdRemito, listaPrecio)
                    tblRemitoServicioDetalle.Insertar(TxtHoras.Text, TxtSubHS.Text, IdRemito, listaPrecio)
                    'esto pasa el estado de  la orden a cumplida
                    TblOrdenServicio.Modificar(HFOrdenServicio.Value, New Date, "", "Cumplida", 0)

                    Me.HFCodigoRemito.Value = IdRemito
                    Me.Response.Write("<script languaje='javascript'>window.alert('Carga del Remito satisfactoria')</script>")
                    limpiarCampos()
                End If
            End If
        Else
            Me.Response.Write("<script languaje='javascript'>window.alert('El Remito ya existe')</script>")
        End If
        'limpiarHF()
    End Sub


    Protected Sub lnkCalcular_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkCalcular.Click
        Dim CantidadKilometros As Double
        Dim CantidadHoras As Double
        HFTUnidad.Value = Request.Form("ctl00$ContentPlaceHolder1$HFTUnidad")
        If TxtKilometros.Text <> "" And TxtHoras.Text <> "" Then
            CantidadKilometros = CDbl(TxtKilometros.Text)
            CantidadHoras = CDbl(TxtHoras.Text)
        End If
        Dim dtsCodigoTunidad As DataSet = New DataSet
        dtsCodigoTunidad = TblTipoUnidad.Buscar(0, HFTUnidad.Value.ToString, 1)
        Dim CodigoTunidad As Long = dtsCodigoTunidad.Tables(0).Rows(0)("codigoTunidad")
        Try
            Dim dtsValores As DataSet = tblValorizar.Valorizar(CantidadKilometros, CantidadHoras, CodigoTunidad)
            TxtTotal.Text = dtsValores.Tables(0).Rows(0)("Total").ToString
            TxtSubKM.Text = dtsValores.Tables(0).Rows(0)("SubKM").ToString
            TxtSubHS.Text = dtsValores.Tables(0).Rows(0)("SubHs").ToString
            HFListaPrecio.Value = dtsValores.Tables(0).Rows(0)("id_ListaPrecio").ToString
        Catch ex As Exception
        End Try
    End Sub

    Protected Sub LinkCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkCancelar.Click
        'limpiarHF()
        limpiarCampos()
    End Sub

    Protected Sub ChkOpcionValorizacion_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkOpcionValorizacion.CheckedChanged
        TxtValorizar.Visible = ChkOpcionValorizacion.Checked
        lblSubtotal.Visible = Not ChkOpcionValorizacion.Checked
        lnkCalcular.Visible = Not ChkOpcionValorizacion.Checked
        TxtSubKM.Visible = Not ChkOpcionValorizacion.Checked
        TxtSubHS.Visible = Not ChkOpcionValorizacion.Checked
        If Not ChkOpcionValorizacion.Checked Then
            TxtSubKM.Text = 0
            TxtSubHS.Text = 0
            '            TxtKilometros.Text = 0
            '            TxtHoras.Text = 0
            RequiredFieldValidator2.ValidationGroup = "Nuevo"
        Else
            RequiredFieldValidator2.ValidationGroup = "NADA"
            TxtValorizar.Text = 0
        End If
        'limpiarHF()
    End Sub

    Protected Sub LinkRemitos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkRemitos.Click
        Response.Redirect("FrmListadoRemitos.aspx")

    End Sub

End Class